//
//  DBSleepData.h
//  CareRingApp
//
//  Created by Linktop on 2022/11/1.
//  睡眠数据的缓存

#import <Foundation/Foundation.h>
#import "StagingDataV2.h"

@class TrendCommonObj;

NS_ASSUME_NONNULL_BEGIN
@class FMDatabase;
@interface DBSleepData : NSObject

@property(strong, nonatomic) NSNumber *sleepStart;
@property(strong, nonatomic) NSNumber *sleepEnd;
@property(strong, nonatomic) NSString *dateString; //格式化时间
@property(strong, nonatomic) NSString *macAddress;

// @property(strong, nonatomic) NSNumber *
@property(strong, nonatomic) NSNumber *restHR; //睡眠前一天的静息心率

@property(strong, nonatomic) NSNumber *hr; //睡眠期间的平均心率
@property(strong, nonatomic) NSNumber *hrv; //睡眠期间的hrv
@property(strong, nonatomic) NSNumber *br; // 睡眠期间的呼吸率
@property(strong, nonatomic) NSNumber *spo2; //睡眠期间的血氧

//@property(strong, nonatomic) NSNumber *ftcBase; //手指温度基准值
//
//@property(strong, nonatomic) NSNumber *frcAvg; //睡眠期间手指温度平均值
//@property(strong, nonatomic) NSNumber *ftcMin; //睡眠期间的手指温度最小值
//@property(strong, nonatomic) NSNumber *ftcMax; //睡眠期间的手指温度最大值

@property(strong, nonatomic) NSNumber *hrDip; //心率沉浸比例 float
@property(strong, nonatomic) NSNumber *duration; //睡眠时长
@property(strong, nonatomic) NSNumber *qulalityDuration; //优质睡眠时长
@property(strong, nonatomic) NSNumber *deepDuration; //深度睡眠时长

@property(strong, nonatomic) NSNumber *effieiency; // 处理wake其他时间和 / 总时间
@property(strong, nonatomic) StagingDataV2 *stagingData;

@property(assign, nonatomic)BOOL isNap;


+(void)createTable;
+(instancetype)copyWithDbSleepData:(DBSleepData *)oriSleep;

+(void)dropTable;

/// 查询最后一次睡眠的缓存
/// - Parameters:
///   - macAddress: 账号
///   - complete: 回调
+(void)queryFirstAndLastTime:(NSString *)macAddress Cmp:(void(^)(NSNumber * lastEndtime, NSNumber * firsttime, NSNumber *firstDataTime))complete;


/// 工厂方法
/// - Parameters:
///   - macAddress: 账号
///   - stagingData: 
+(instancetype)dbDataWithmacAddress:(NSString *)macAddress StagingData:(StagingDataV2 *)stagingData;


+(void)queryDbSleepBy:(NSString *)macAddress Begin:(NSTimeInterval)beginTime EndTime:(NSTimeInterval)endTime Comp:(void(^)(NSMutableArray<DBSleepData *> *results))cmpBlk;

/// 其他的计算并保存
/// - Parameters:
///   - restHr: 静息心率
///   - complete: 完成回调
-(void)doOtherCalcAndSaveComplete:(void(^)(BOOL succ))complete;


+(void)queryDbSleepOrderBySleepEndBy:(NSString *)macAddress Begin:(NSTimeInterval)beginTime EndTime:(NSTimeInterval)endTime Comp:(void(^)(NSMutableArray<DBSleepData *> *results))cmpBlk;

+(void)queryDbAvgSleepOrderBy:(NSString *)macAddress Ranges:(NSMutableArray <TrendCommonObj *> *)commonObjs Comp:(void(^)(void))cmpBlk;

// 傻逼二级页面使用
+(void)queryDbAvgSleepBy:(NSString *)macAddress
                   Begin:(NSDate *)beginDate
                     End:(NSDate *)endDate
                    Comp:(void(^)(DBSleepData * avgSleepData))cmpBlk;

+(NSNumber *)queryLastFtcBaseSync:(NSString *)macAddress DB:(FMDatabase * _Nonnull) db From:(NSDate *)beginDate End:(NSDate *)endDate;

// 同一天的合并
-(void)appendSleep:(DBSleepData *)newSleepData;



+(void)testDel:(NSString *)macAddress Endtime:(NSString *)timeString CMP:(void(^)(BOOL succ))complete;


-(void)insert:(void(^)(BOOL succ))complete;

/// 查询最后一次睡眠的缓存,仅用于睡眠计算前的查询, 会删除最后一条记录
/// - Parameters:
///   - macAddress: 账号
///   - complete: 回调
+(void)queryFirstAndLastTimeOnlyForSleepCalc:(NSString *)macAddress Cmp:(void(^)(NSNumber * lastEndtime, NSNumber * firsttime, NSNumber *firstDataTime))complete;


+(void)clearColumnBy:(NSString *)macAddress DB:(FMDatabase * _Nonnull )db;

+(void)debuginsert:(NSMutableArray<NSDictionary *> *)dataArray MacAddress:(NSString *)macAddress CMP:(void(^)(BOOL succ))complete;
@end

NS_ASSUME_NONNULL_END
